package com.huawei.chengdu.meetingdemo.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.device.idea.meeting.MultiStream.model.LayoutInfo;
import com.device.idea.meeting.MultiStream.model.LayoutMode;
import com.device.idea.meeting.MultiStream.model.LayoutSubWindow;
import com.device.idea.meeting.MultiStream.model.StreamType;
import com.device.idea.meeting.MultiStream.model.SurfaceViewInfo;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.consts.ConfConst;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;
import com.huawei.chengdu.meetingdemo.util.ScreenUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InMeetingFragmentFixed extends Fragment {
    private static final String TAG = InMeetingFragmentFixed.class.getSimpleName();
    public static final Map<Integer, Integer> SURFACE_VIEWID_MAP = new HashMap<>();
    private final List<SurfaceViewInfo> surfaceViewInfos = new ArrayList<>();
    private PipWindowView pipWindowView;
    private View view;
    private List<SurfaceView> surfaceViewList = new ArrayList<>(3);
    private LayoutInfo layoutInfo;
    private LayoutMode curLayoutMode;

    public InMeetingFragmentFixed(LayoutInfo layoutInfo) {
        super();
        this.layoutInfo = layoutInfo;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_in_meeting_layout_fixed, container, false);
        initView();
        handleLayoutChangeNotify();
        return view;
    }

    private void initView() {
        surfaceViewList.add(view.findViewById(R.id.surface_view_1));
        surfaceViewList.add(view.findViewById(R.id.surface_view_2));
        surfaceViewList.add(view.findViewById(R.id.surface_view_3));
    }

    public void handleLayoutChangeNotify() {
        surfaceViewInfos.clear();
        curLayoutMode = layoutInfo.getWindowsInfo().getLayoutMode();
        switch (curLayoutMode) {
            case FULL_SCREEN: /* 全屏 */
            case PIP_LEFT_TOP: /* 画中画 左上 */
            case PIP_RIGHT_TOP: /* 画中画 右上 */
            case PIP_LEFT_BOTTOM: /* 画中画 左下 */
            case PIP_RIGHT_BOTTOM: /* 画中画 右下 */
                handlePipScreenView();
                break;
            case SPLIT_SCREEN: /* 二分屏 */
            case SPLIT_LBRS_SCREEN: /* 二分屏左大右小 */
            case SPLIT_UBBS_SCREEN: /* 二分屏上大下小 */
                handleSplitScreenView();
                break;
            case THREE_SPLIT_SCREEN_LB1RS2: /* 三分屏 左1大 + 右2小 */
            case THREE_SPLIT_SCREEN_UB1BS2: /* 三分屏 上1大 + 下2大 */
                handleThreeSplitScreenView(curLayoutMode);
                break;
            case VIP_LBRS: /* VIP 左大 + 右小 */
            case VIP_UBBS: /* VIP 上大 + 下小 */
            case VIP_USBB: /* VIP 上小 + 下大 */
            case GALLERY: /* 画廊 */
                break;
            default:
                break;
        }
    }

    private void handlePipScreenView() {
        // 重置surfaceView大小  pip或全屏模式下，隐藏二分屏、三分屏的surfaceView
        setPositionAndSize(surfaceViewList.get(0), 0, 0, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        surfaceViewList.get(1).setVisibility(View.GONE);
        surfaceViewList.get(2).setVisibility(View.GONE);

        // 处理主画面
        LayoutSubWindow subWindowMain = layoutInfo.getWindowsInfo().getSubWindows().get(0);
        // 目标码流中本地投屏码流不用绑定ViewId
        if (ConfConst.TARGET_STEAM_TYPE.contains(subWindowMain.getStreamType()) &&
                subWindowMain.getStreamType() != StreamType.STREAM_WIRED_PROJECTION &&
                subWindowMain.getStreamType() != StreamType.STREAM_WIRELESS_PROJECTION) {
            SurfaceViewInfo surfaceViewInfo = bindSurface(subWindowMain, surfaceViewList.get(0));
            surfaceViewInfos.add(surfaceViewInfo);
        }

        // 处理PIP画面
        if (layoutInfo.getWindowsInfo().getSubWindows().size() > 1) {
            if (pipWindowView == null) {
                pipWindowView = new PipWindowView(getContext());
            }
            pipWindowView.setCurLayoutMode(curLayoutMode);
            pipWindowView.show();
            LayoutSubWindow subWindowSmall = layoutInfo.getWindowsInfo().getSubWindows().get(1);
            SurfaceViewInfo surfaceViewInfo = bindSurface(subWindowSmall, pipWindowView.surfaceViewPip);
            surfaceViewInfos.add(surfaceViewInfo);
        } else if (pipWindowView != null){
            pipWindowView.hide();
        }
        ConfSdkPresenter.confirmViewCreateSuccess(surfaceViewInfos);
    }

    private void handleSplitScreenView() {
        // 隐藏pip小画面
        if (pipWindowView != null) {
            pipWindowView.hide();
        }

        // 重置surfaceView大小  二分屏模式下，隐藏三分屏的surfaceView
        setPositionAndSize(surfaceViewList.get(0), ConfConst.SPLIT_LEFT, ConfConst.SPLIT_Y, ConfConst.SPLIT_WIDTH, ConfConst.SPLIT_HEIGHT);
        setPositionAndSize(surfaceViewList.get(1), ConfConst.SPLIT_RIGHT, ConfConst.SPLIT_Y, ConfConst.SPLIT_WIDTH, ConfConst.SPLIT_HEIGHT);
        surfaceViewList.get(2).setVisibility(View.GONE);

        for (int index = 0; index < 2; index++) {
            LayoutSubWindow subWindow = layoutInfo.getWindowsInfo().getSubWindows().get(index);
            SurfaceViewInfo surfaceViewInfo = bindSurface(subWindow, surfaceViewList.get(index));
            surfaceViewInfos.add(surfaceViewInfo);
        }
        ConfSdkPresenter.confirmViewCreateSuccess(surfaceViewInfos);
    }

    private void handleThreeSplitScreenView(LayoutMode layoutMode) {
        // 隐藏pip小画面
        if (pipWindowView != null) {
            pipWindowView.hide();
        }
        // 重置surfaceView大小  三分屏有两种布局模式，需要分别设置surfaceView大小
        if (layoutMode == LayoutMode.THREE_SPLIT_SCREEN_LB1RS2) {
            setPositionAndSize(surfaceViewList.get(0), ConfConst.THREE_SPLIT_LB1RS2_LEFT_X, ConfConst.THREE_SPLIT_LB1RS2_LEFT_Y,
                    ConfConst.THREE_SPLIT_BIG_WIDTH, ConfConst.THREE_SPLIT_BIG_HEIGHT);
            setPositionAndSize(surfaceViewList.get(1), ConfConst.THREE_SPLIT_LB1RS2_RIGHT_X, ConfConst.THREE_SPLIT_LB1RS2_RIGHT_TOP_Y,
                    ConfConst.THREE_SPLIT_SAMLL_WIDTH, ConfConst.THREE_SPLIT_SMALL_HEIGHT);
            setPositionAndSize(surfaceViewList.get(2), ConfConst.THREE_SPLIT_LB1RS2_RIGHT_X, ConfConst.THREE_SPLIT_LB1RS2_RIGHT_BOTTOM_Y,
                    ConfConst.THREE_SPLIT_SAMLL_WIDTH, ConfConst.THREE_SPLIT_SMALL_HEIGHT);
        } else {
            setPositionAndSize(surfaceViewList.get(0), ConfConst.THREE_SPLIT_UB1BS2_UP_X, ConfConst.THREE_SPLIT_UB1BS2_UP_Y,
                    ConfConst.THREE_SPLIT_BIG_WIDTH, ConfConst.THREE_SPLIT_BIG_HEIGHT);
            setPositionAndSize(surfaceViewList.get(1), ConfConst.THREE_SPLIT_UB1BS2_BELOW_LEFT_X, ConfConst.THREE_SPLIT_UB1BS2_BELOW_Y,
                    ConfConst.THREE_SPLIT_SAMLL_WIDTH, ConfConst.THREE_SPLIT_SMALL_HEIGHT);
            setPositionAndSize(surfaceViewList.get(2), ConfConst.THREE_SPLIT_UB1BS2_BELOW_RIGHT_X, ConfConst.THREE_SPLIT_UB1BS2_BELOW_Y,
                    ConfConst.THREE_SPLIT_SAMLL_WIDTH, ConfConst.THREE_SPLIT_SMALL_HEIGHT);
        }
        for (int index = 0; index < 3; index++) {
            LayoutSubWindow subWindow = layoutInfo.getWindowsInfo().getSubWindows().get(index);
            SurfaceViewInfo surfaceViewInfo = bindSurface(subWindow, surfaceViewList.get(index));
            surfaceViewInfos.add(surfaceViewInfo);
        }
        ConfSdkPresenter.confirmViewCreateSuccess(surfaceViewInfos);
    }

    public void setLayoutInfo(LayoutInfo layoutInfo) {
        this.layoutInfo = layoutInfo;
    }

    private void setPositionAndSize(View view, int x, int y, int width, int height) {
        view.setVisibility(View.VISIBLE);
        view.setX(ScreenUtil.getDimensionSize(x));
        view.setY(ScreenUtil.getDimensionSize(y));
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = width;
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }

    public SurfaceViewInfo bindSurface(LayoutSubWindow subWindow, SurfaceView surface) {
        SurfaceViewInfo surfaceViewInfo = new SurfaceViewInfo();
        surfaceViewInfo.setViewId(subWindow.getViewId());
        surfaceViewInfo.setSurfaceView(surface);
        SURFACE_VIEWID_MAP.put(surface.getId(), subWindow.getViewId());
        return surfaceViewInfo;
    }
}
